import Mock from 'mockjs'
import user from './user'
import search from './search'
import admin from './admin'
import blog from './blog'

export function mockXHR() {
  Mock.XHR.prototype.proxy_send = Mock.XHR.prototype.send
  Mock.XHR.prototype.send = function() {
    if (this.custom.xhr) {
      this.custom.xhr.withCredentials = this.withCredentials || false
      this.custom.xhr.responseType = this.responseType
    }
    this.proxy_send(...arguments)
  }

  // 设置请求头支持
  Mock.setup({
    timeout: '200-600',
    withCredentials: true
  })

  const mocks = [
    ...user,
    ...search,
    ...admin,
    ...blog
  ]

  for (const i of mocks) {
    Mock.mock(new RegExp(i.url), i.method || 'get', (options) => {
      const { body, type, url, headers } = options
      console.log('mock options:', options)
      return i.response({
        body,
        method: type,
        query: url,
        headers: options.headers || {}
      })
    })
  }
} 